package com.suredy.app.project.ctrl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.project.model.Project;
import com.suredy.app.project.model.ProjectCost;
import com.suredy.app.project.service.ProjectCostSrv;

/**
 * 费用支出列表控制器
 * 
 * @author VIVID.G
 * @since 2015-7-30
 * @version v0.1
 */
@Controller
@RequestMapping("/cost/list")
public class CostListCtrl {

	@Autowired
	private ProjectCostSrv costSrv;

	@RequestMapping(value = "/{projId}")
	public String list(@PathVariable String projId, String key, Model model) {
		String view = "app/project/form-cost";

		if (StringUtils.isBlank(projId))
			return view;

		ProjectCost search = new ProjectCost();
		search.setProject(new Project());
		search.getProject().setId(projId);
		search.setName(key);

		List<ProjectCost> data = this.costSrv.readByEntity(search);

		// 模拟数据库对象
		Project proj = search.getProject();

		model.addAttribute("data", search.getProject());
		model.addAttribute("key", key);

		if (data == null || data.isEmpty())
			return view;

		proj.setCosts(data);

		for (ProjectCost c : data) {
			c.setProject(proj);
		}

		return view;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public boolean deleteCostForm(String[] id) {
		if (id == null || id.length <= 0)
			return false;

		return this.costSrv.deleteWithIds(id);
	}

}
